﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TrangestAdmin
{
    public partial class Clientes : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void uxFiltro_TextChanged(object sender, EventArgs e)
        {
            aplicaFiltro();
            uxTablaclientes.DataBind();
        }

        private void aplicaFiltro()
        {
            sqlclientes.FilterParameters.Clear();
            if (uxFiltro.Text.Length == 0 && uxFiltroMarca.Text.Length == 0)
            {
                sqlclientes.FilterExpression = null;
            }
            else
            {
                ControlParameter cp = new ControlParameter();
                cp = new ControlParameter();
                cp.ControlID = uxFiltro.ID.ToString();
                cp.PropertyName = "Text";
                cp.Name = "filtrodoc";
                cp.Type = TypeCode.String;
                cp.ConvertEmptyStringToNull = false;

                ControlParameter cpMarca = new ControlParameter();
                cpMarca = new ControlParameter();
                cpMarca.ControlID = uxFiltroMarca.ID.ToString();
                cpMarca.PropertyName = "Text";
                cpMarca.Name = "filtronombre";
                cpMarca.Type = TypeCode.String;
                cpMarca.ConvertEmptyStringToNull = false;

                if (uxFiltroMarca.Text.Length > 0 && uxFiltro.Text.Length == 0)
                {
                    sqlclientes.FilterExpression = "nombre_cliente like '%{0}%'";
                    sqlclientes.FilterParameters.Add(cpMarca);
                }
                else if (uxFiltroMarca.Text.Length > 0 && uxFiltro.Text.Length > 0)
                {
                    sqlclientes.FilterExpression = "(nombre_cliente like '%{0}%') and (documento_ruc like '%{1}%')";
                    sqlclientes.FilterParameters.Add(cpMarca);
                    sqlclientes.FilterParameters.Add(cp);
                }
                else if (uxFiltroMarca.Text.Length == 0 && uxFiltro.Text.Length > 0)
                {
                    sqlclientes.FilterExpression = "documento_ruc like '%{0}%'";
                    sqlclientes.FilterParameters.Add(cp);
                }
            }
        }

        protected void uxFiltroMarca_TextChanged(object sender, EventArgs e)
        {
            aplicaFiltro();
            uxTablaclientes.DataBind();

        }

        protected void uxTablaclientes_RowEditing(object sender, GridViewEditEventArgs e)
        {
            uxTablaclientes.EditIndex = e.NewEditIndex;
            aplicaFiltro();
            uxTablaclientes.DataBind();
        }

        protected void uxTablaclientes_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            finalizaEditDeleteCancel();
        }

        protected void uxTablaclientes_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            finalizaEditDeleteCancel();
        }

        protected void uxInsertNew_Click(object sender, EventArgs e)
        {
            sqlclientes.Insert();
            finalizaEditDeleteCancel();
        }

        protected void uxTablaclientes_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (uxTablaclientes.EditIndex != -1)
            {
                if (e.Row.RowIndex == uxTablaclientes.EditIndex)
                {
                    ImageButton myBtn = (ImageButton)e.Row.FindControl("btnDelete");
                    if (myBtn != null)
                    {
                        myBtn.Enabled = false;
                        myBtn.Visible = false;
                    }
                }
                
            }

        }

        protected void uxTablaclientes_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            finalizaEditDeleteCancel();
        }

        private void finalizaEditDeleteCancel()
        {
            uxTablaclientes.EditIndex = -1;
            aplicaFiltro();
            uxTablaclientes.DataBind();
        }
    }
}